package com.lime49.lockcrypt.ui;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.*;
import java.awt.event.*;

public class DropDownButton extends JButton implements ActionListener {
    private JButton mainButton, dropDownButton;
    private JPopupMenu menu;
    
    /**
    * Creates a drop down button with no menu or text
    */
    public DropDownButton() {
        this(" ");
    }
    
    /**
    * Creates a drop down button with the specified text
    * @param text The text to display
    */
    public DropDownButton(String text) {
        this(text, SwingConstants.SOUTH);
    }
    
    /**
    * Creates a drop down button with an arrow pointing in the specified direction and the specified text
    * @param text The text to display
    * @param direction A SwingConstant direction indicatiing the arrow direction
    */
    public DropDownButton(String text, int direction) {
        this(new JButton(text), direction, new JPopupMenu());
    }
    
    /**
    * Creates a drop down button using the specified button and popup menu
    * @param mainButton The main button to use
    * @param direction A SwingConstant representing the direction of the arrow
    * @param menu The drop down menu to show when the arrow is clicked
    */
    public DropDownButton(JButton mainButton, int direction, JPopupMenu menu) {
        super();
        this.mainButton = mainButton;
        this.dropDownButton = new BasicArrowButton(direction);
        this.dropDownButton.addActionListener(this);
        
        setLayout(new BorderLayout());
        setMargin(new Insets(0,0,0,0));
        add(mainButton, BorderLayout.CENTER);
        add(dropDownButton, BorderLayout.EAST);
        this.setBorder(mainButton.getBorder());
        this.dropDownButton.setBorder(BorderFactory.createEtchedBorder());
        this.mainButton.setBorderPainted(false);
        this.mainButton.setBorder(null);
        mainButton.setContentAreaFilled(false);
        dropDownButton.setContentAreaFilled(false);
        this.menu = menu;
        
        int prefHeight = mainButton.getPreferredSize().height;
        int prefWidth = mainButton.getPreferredSize().width;
        Dimension wholeSize = new Dimension(prefWidth+30,prefHeight+12);
        
        this.setMinimumSize(wholeSize);
        this.setPreferredSize(wholeSize);
        this.setMaximumSize(wholeSize);
    }
    
    /**
    * Enables or disables this button and it's menu
    */
    @Override
    public void setEnabled(boolean enabled) {
        this.mainButton.setEnabled(enabled);
        this.dropDownButton.setEnabled(enabled);
    }
    
    /**
    * Returns the menu assigned to this drop down button
    * @return The menu for this drop down button
    */
    public JPopupMenu getMenu() {
        return menu;
    }
    
    /**
    * Displays the menu at the location of the ActionEvent, e
    * @param e The ActionEvent around which to center the menu
    */
    public void showMenu(ActionEvent e) {
        if(menu == null) { // no menu's been set
            return;
        } else {
            if(!menu.isVisible()){
              Point p = this.getLocationOnScreen();
              menu.setLocation((int)p.getX()+mainButton.getWidth(),(int)p.getY()+this.getHeight());
              menu.pack();
              menu.setVisible(true);
              menu.requestFocusInWindow();
            } else { //if menu is already show, hide it
              menu.setVisible(false);
            }
        }
    }
    
    /**
    * The ActionListener to register on the button to display the menu
     * @param e 
     */
    public void actionPerformed(ActionEvent e) {
        showMenu(e);
    }
    @Override
    public void addActionListener(ActionListener a) {
        this.addActionListener(a);
        mainButton.addActionListener(a);
    }
}
